<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function index() {
        //redireccion segura
        if ($this->session->userdata('access')) {
            redirect('xpanel');
        }
        $this->load->view('login/login');
    }

    private function verifica($usuario, $pass) {
        $result = $this->rh_model->verificar_datos($usuario, $pass);
        #existe usuario
        if (is_object($result)) {
            $admin['u_usuario_id'] = $result->u_usuario_id;
            $admin['a_nombre'] = $result->a_nombre;
            $admin['a_email'] = $result->a_email;
            $admin['tu_tipo_usuario'] = $result->tu_tipo_usuario;
            $this->session->set_userdata('admin', $admin);
            return true;
        }
        # datos no existentes
        return false;
    }

    public function x() {

        # SET VALIDATION RULES
        $this->form_validation->set_rules('user', 'username', 'required|trim|xss_clean|min_length[6]|max_length[15]');
        $this->form_validation->set_rules('pass', 'password', 'required|trim|xss_clean|min_length[6]|max_length[20]');

        # has the form been submitted and with valid form info (not empty values)
        if (!$this->form_validation->run()) {
            redirect('sushitto');
        }

        # autenticacion segura
        $usuario = $this->security->xss_clean($this->input->post('user'));
        $pass = $this->security->xss_clean($this->input->post('pass'));

        if ($this->verifica($usuario, $pass)) {
            $admin = $this->session->userdata('admin');
            switch ($admin['tu_tipo_usuario']) {
                #activador
                case 1:
                    redirect('xpanel');
                    break;
                #tipo usuario 2
                case 2:
                    redirect('tipo2');
                    break;
                #tipo usuario 3
                case 3:
                    redirect('tipo3');
                    break;
                #tipo usuario 4
                case 4:
                    redirect('tipo4');
                    break;
            }
        } else {
            redirect('welcome');
        }
    }

}
